<script type="text/javascript" src="lib/checkbrowser.js"></script>
<script type="text/javascript">
checkBrowser(deny, warn, pass);
</script>
<div id="header">
  <div id="topnav">
  <%if CheckLogin() then%>
    <label id="user"><a id="changepws" href="#"><%=session("sFullName")%></a></label>
    ，<a id="logout" href="#">退出</a>
  <%
    Set rs_mytodo = Server.CreateObject("ADODB.Recordset")
    sql = "SELECT iif(sum(iif(complete=true,1,0)),sum(iif(complete=true,1,0)),0) as done,iif(sum(iif(complete=false,1,0)),sum(iif(complete=false,1,0)),0) as undone FROM todo where userid="&session("iID")&" and active_date=date() and deleted=false"
    rs_mytodo.Open sql,conn,1,1
    if not rs_mytodo.eof then
        today_mydone=rs_mytodo("done")
        today_myundone=rs_mytodo("undone")
        todya_mytodos=today_mydone+today_myundone
    end if
    rs_mytodo.close
    sql = "SELECT count(*) as mytodos FROM todo where userid="&session("iID")&" and complete=false and deleted=false"
    rs_mytodo.Open sql,conn,1,1
    if not rs_mytodo.eof then
        all_mytodos=rs_mytodo("mytodos")
    end if
    rs_mytodo.close
    set rs_mytodo=nothing
  %>
  
  <div><a href="#" class="view_todaytodos">我今天有 <%=today_myundone%> / <%=todya_mytodos%> 个任务</a>， 还有 <%=all_mytodos%> 个待办任务</div>
  <%else%>
    <a id="login" href="#">登录</a>
  <%end if%>
  
  </div>
  <h1><%=cop.item("copname")%></h1>
</div>
<div id="dv_changepws" title="修改密码">
  <form id="fm_changepws" method="post" class="yform full" role="application">
    <div class="type-text">
      <label for="old_password">原密码*</label>
      <input name="old_password" id="old_password" type="password" maxlength="10" class="validate[required,minSize[6],maxSize[10]]" />
    </div>
    <div class="type-text">
      <label for="new_password">新密码*</label>
      <input name="new_password" id="new_password" type="password" maxlength="10" class="validate[required,minSize[6],maxSize[10],custom[onlyPassword]]" />
    </div>
    <div class="type-text">
      <label for="confirm_password">确认密码*</label>
      <input name="confirm_password" id="confirm_password" type="password" maxlength="10" class="validate[required,minSize[6],maxSize[10],custom[onlyPassword]]" />
    </div>
  </form>
</div>
<div id="dv_login" title="登录界面">
  <form id="fm_login" method="post" class="yform full" role="application">
    <div class="type-text">
      <label for="login_username">用户名*</label>
      <input name="login_username" id="login_username" type="text" maxlength="10" class="validate[required,minSize[3],maxSize[10]]" />
    </div>
    <div class="type-text">
      <label for="login_password">密码*</label>
      <input name="login_password" id="login_password" type="password" maxlength="10" class="validate[required,minSize[6],maxSize[10]]" />
    </div>
  </form>
</div>
<div id="dv_todo" title="任务界面">
  <form id="fm_todo" method="post" class="yform full" role="application">
    <div class="type-text">
      <label for="todo_title">任务标题*</label>
      <input name="todo_title" id="todo_title" type="text" class="validate[required,maxSize[20]]" maxlength="20" />
      <input name="todo_id" id="todo_id" type="hidden" value="0" />
    </div>
    <div class="type-text">
      <label for="todo_active">提醒日期<font color="#f66">（建议填写，到期会自动提醒）</font></label>
      <input name="todo_active" id="todo_active" type="text" size="10" readonly="readonly" />
    </div>
    <div class="type-check">
        <input id="todo_star" name="todo_star" type="checkbox" />
        <label for="todo_star">重要<font color="#f66">（重要的任务会放在前面）</font></label>
    </div>

    <div class="type-text">
        <label for="todo_content">任务内容</label>
        <textarea name="todo_content" id="todo_content" rows="4"></textarea>
    </div>
  </form>
</div>

<script type="text/javascript">
  $(function() {
    function checkchangepws(o,n,c){
      if(o==""){
        r=1
      }else if(n==""|c==""){
        r=2
      }else if(o.length<6||n.length<6||c.length<6){
        r=3
      }else if(n!=c){
        r=4
      }else if(o==n){
        return 5
      }else if(!checkpws(n)){
        r=6
      }else{
        r=0
      }
      return r
    }
    $("#login").click(function(){
      $("#dv_login").dialog("open");
    });
    $("#changepws").click(function(){
      $("#dv_changepws").dialog("open");
    })
    $("#dv_changepws").dialog({
      autoOpen: false,
      minHeight:200,
      modal: true,
      buttons: {
        "确定": function() {
          if($("#fm_changepws").validationEngine('validate')){
            $.post("changepws.asp",{action:"确定",old_password:$.md5($("#old_password").val()),new_password:$.md5($("#new_password").val()),confirm_password:$.md5($("#confirm_password").val()),checkchangepws:checkchangepws($("#old_password").val(),$("#new_password").val(),$("#confirm_password").val())},function(data){
              if(data){
                alert(data.sInfo);
                if(data.bStatus){
                  if($("#url").val()){
                    location.href=$("#url").val();
                  }else{
                    location.href="index.asp";
                  }
                }
              }
            },"json");
          }
        },
        "取消": function() {
          $(this).dialog( "close" );
        }

      }
    });
    $("#dv_login").dialog({
      autoOpen: false,
      minHeight:200,
      modal: true,
      buttons: {
        "登录": function() {
          $.post("login.asp",{action:"登录",username:$("#login_username").val(),password:$.md5($("#login_password").val()),checkpws:checkpws($("#login_password").val())},function(data){
            if(data.bStatus){
              if(data.bChangePws){
                alert(data.sInfo);
                location.href="index.asp";
              }else{
                if($("#url").val()){
                  location.href=$("#url").val();
                }else{
                  location.href="index.asp";
                }
              }
            }else{
              alert(data.sInfo);
            }
          },"json");
        },
        "取消": function() {
          $(this).dialog( "close" );
        }

      }
    });
    
    $(".create_newtodo").click(function(){
      $("#fm_todo")[0].reset();
      $("#todo_id").val("0");
      $("#dv_todo").dialog("open");
    })
    $("#todo_active").datepicker({ minDate: 0 });    
    $("#dv_todo").dialog({
        autoOpen: false,
        minHeight:200,
        //width:400,
        modal: true,
        position: { 
            my: "center center", at: "center center" 
        },
        buttons: {
            "保存": function() {
                if($("#fm_todo").validationEngine('validate')){
                    $.post("json_todo.asp?"+$("#fm_todo").serialize(),{action:"保存",cd:$("#id").val()},function(data){
                        alert(data.sInfo);
                        if(data.bStatus){
                            //alert($(".create_newtodo").data("foo"));
                            //alert($(".create_newtodo").attr("data-foo"));
                            $("#dv_todo").dialog( "close" );
                            if($(".create_newtodo").data("foo")=="todomain"){
                                $("#view_allundonetodo").trigger("click");
                            }else{
                            location.reload();
                            }
                        }
                    },"json");
                }
            },
            "取消": function() {
                $(this).dialog( "close" );
            }

        }
    });

    $("#dv_todolist a.todo_done,#dv_todos a.todo_done").live('click',function(e){
        e.preventDefault();
        if(confirm("确定完成这个任务吗？")){
            var $cur_li=$(this).closest('li');
            var $cur_ol=$cur_li.closest('ol');
            $.getJSON('json_todo.asp',{id:$cur_li.data('foo'),action:'完成'},function(data){
                //alert(data.sInfo);
                if(data.bStatus){
                    //location.reload();
                    $cur_li.find('span.todo_title')
                        .addClass('complete');
                    $cur_li.find('a.todo_done,a.todo_edit,a.todo_dele,a.todo_redo')
                        .toggleClass('todo_hide');
                        
                    $cur_li.insertAfter($cur_ol.find('.todo_title').not('.complete').last().parent());
                    if(!$cur_li.find('span.todo_title').hasClass('star')){
                        if($cur_ol.find('.todo_title.complete.star')){
                            $cur_li.insertAfter($cur_ol.find('.todo_title.complete.star').last().parent());
                        }
                        
                    }

                    
                }else{
                    alert(data.sInfo);
                }
            },"json");
        }
    });
    $("#dv_todolist a.todo_redo,#dv_todos a.todo_redo").live('click',function(e){
        e.preventDefault();
        if(confirm("确定重启这个任务吗？")){
            var $cur_li=$(this).closest("li");
            var $cur_ol=$cur_li.closest('ol');
            $.getJSON("json_todo.asp",{id:$cur_li.data("foo"),action:"重启"},function(data){
                //alert(data.sInfo);
                if(data.bStatus){
                    //location.reload();
                    $cur_li.find('span.todo_title')
                        .removeClass('complete');
                    $cur_li.find('a.todo_done,a.todo_edit,a.todo_dele,a.todo_redo')
                        .toggleClass('todo_hide');

                    $cur_ol.prepend($cur_li);
                    if(!$cur_li.find('span.todo_title').hasClass('star')){
                        $cur_li.insertAfter($cur_ol.find('.todo_title.star').not('.complete').last().parent());
                    }
                }else{
                    alert(data.sInfo);
                }
            },"json");
        }
    });
    $("#dv_todolist a.todo_dele,#dv_todos a.todo_dele").live('click',function(e){
        e.preventDefault();
        if(confirm("确定删除这个任务吗？")){
            var $cur_li=$(this).closest("li");
            $.getJSON("json_todo.asp",{id:$cur_li.data("foo"),action:"删除"},function(data){
                //alert(data.sInfo);
                if(data.bStatus){
                    //location.reload();
                    $cur_li.remove();
                }else{
                    alert(data.sInfo);
                }
            },"json");
        }
    });
    function fn_todo_edit(id){
        $.getJSON("json_todo.asp",{id:id,action:"编辑"},function(data){
            //alert(data.sInfo);
            if(data.bStatus){
                //location.reload();
                $("#todo_id").val(data.iTodoID);
                $("#todo_title").val(data.sTodoTitle);
                $("#todo_content").val(data.sTodoContent);
                $("#todo_active").val(data.sTodoActive);
                if(data.bTodoStar){
                    $("#todo_star").prop("checked","checked");
                }else{
                    $("#todo_star").prop("checked","");
                }
                $("#dv_todo").dialog("open");
            }else{
                alert(data.sInfo);
            }
        },"json");
    
    }
    $("#tb_todolist a.todo_edit").live('click',function(e){
        e.preventDefault();
        fn_todo_edit($(this).closest('tr').find(".checkid").val());
    });
    $("#dv_todolist a.todo_edit,#dv_todos a.todo_edit").live('click',function(e){
        e.preventDefault();
        var $cur_li=$(this).closest("li");
        fn_todo_edit($cur_li.data("foo"));
    });
    $("#dv_todolist a.todo_cdno,#dv_todos a.todo_cdno").live('click',function(e){
        e.preventDefault();
        var $cur_li=$(this).closest("li");
        window.open("process.asp?id="+$cur_li.data("cd"));
    });

    $(".view_todaytodos").click(function(){
        location.href='todo.asp';
    });
    


    $("#logout").click(function(){
      $.post("login.asp",{action:"退出"},function(data){
        if(data.bStatus){
          location.href="index.asp";
        }
      },"json");
    });
    //登录界面支持回车提交
    $("#fm_login").keypress(function(event){
        if(event.which==13){
            //event.preventDefault();
            $(".ui-button:contains('登录')").click();
        }
    });
    //修改密码界面支持回车提交
    $("#fm_changepws").keypress(function(event){
        if(event.which==13){
            //event.preventDefault();
            $(".ui-button:contains('确定')").click();
        }
    });
    //任务界面支持回车提交
    $("#fm_todo").keypress(function(event){
        if(event.which==13){
            //event.preventDefault();
            $(".ui-button:contains('保存')").click();
        }
    });

  });
</script>
